<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <!-- 引入Element UI的CSS -->
    <link rel="stylesheet" href="/css/index.css">
    <!-- 引入Vue.js -->
    <script src="/js/vue.js"></script>
    <!-- 引入Element UI的JS -->
    <script src="/js/index.js"></script>
    <!-- 引入Axios -->
    <script src="/js/axios.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: linear-gradient(135deg, #6e8efb, #a777e3);
        }
        .login-container {
            width: 400px;
            padding: 40px;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 10px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
        }
        .login-title {
            text-align: center;
            color: #303133;
            font-size: 32px;
            margin-bottom: 30px;
            font-weight: bold;
            letter-spacing: 2px;
        }
        .el-form-item__label {
            font-size: 16px;
            font-weight: bold;
        }
        .el-button--primary {
            background-color: #6e8efb;
            border-color: #6e8efb;
            transition: all 0.3s ease;
        }
        .el-button--primary:hover {
            background-color: #5a7de2;
            border-color: #5a7de2;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="login-container">
        <h3 class="login-title">用户登录</h3>
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" show-password prefix-icon="el-icon-lock"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleLogin('loginForm')" style="width: 100%;">登录</el-button>
            </el-form-item>

            <!-- 新增小字链接 -->
            <div style="text-align: right; margin-top: 10px;">
                <a href="loginup.html" style="font-size: 14px; color: #6e8efb; text-decoration: none;">立即注册</a>
            </div>
        </el-form>
    </div>
</div>

<script>
new Vue({
    el: '#app',
    data() {
        return {
            loginForm: {
                username: 'admin',
                password: 'tiger'
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleLogin(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // 构造要提交的数据对象，确保参数名与后端一致
                    const params = {
                        username: this.loginForm.username,
                        password: this.loginForm.password
                    };
                    
                    axios.get('/user/login', { params })
                        .then(response => {
                            const result = response.data;
                            if (result.code === 200) {
                                this.$message.success(result.msg || '登录成功');
                                window.location.href = '/html/menu.html';
                            } else {
                                this.$message.error(result.msg || '登录失败，请检查用户名或密码');
                            }
                        })
                        .catch(error => {
                            console.error('登录请求出错:', error);
                            this.$message.error('网络错误，请稍后重试');
                        });
                } else {
                    console.log('表单验证失败');
                    return false;
                }
            });
        }
    }
});
</script>
</body>
</html>